
package com.idea.rdftree;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.tree.TreeNode;

//import org.idea.common.vocabularies.FILESYSTEM;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.idea.common.rdf.RdfUtils;
import com.idea.resources.tree.TreeIcons;

public class ModelNode extends ResourceNode implements RdfTreeNode {
	private Resource resource;
	private RdfNodeMap nodeMap;
	private List children;

	private Model model;

	/**
	 * 
	 * 
	 */
	public ModelNode(
		RdfNodeMap nodeMap,
		Model model,
		Statement parentStatement,
		Resource resource) {
		super(nodeMap, parentStatement, resource); // @@TODO
		this.resource = resource;
		this.nodeMap = nodeMap;
		this.model = model;

		//String filename = RdfUtils_.getFirstPropertyValue(resource, FILESYSTEM.local).toString();
		//setTreeModel(treeModel);
	}

	/* (non-Javadoc)
	 * @see javax.swing.tree.TreeNode#getChildAt(int)
	 */
	public TreeNode getChildAt(int childIndex) {
		//    System.out.println("\nres.getChildAt " + childIndex);

		//  System.out.println("MODEL.getChildAt = " +
		//    (TreeNode)getChildren().get(childIndex));

		return (TreeNode) getChildren().get(childIndex);
	}

	/* (non-Javadoc)
	 * @see javax.swing.tree.TreeNode#getChildCount()
	 */
	public int getChildCount() {
		return getChildren().size();
	}

	/* (non-Javadoc)
	 * @see javax.swing.tree.TreeNode#getParent()
	 */
	public TreeNode getParent() {
		return nodeMap.getTreeNode(
			RdfUtils.getParent(getModel(), resource));
	}

	/* (non-Javadoc)
	 * @see javax.swing.tree.TreeNode#getIndex(TreeNode)
	 */
	public int getIndex(TreeNode node) {
		return getChildren().indexOf(node);
	}

	/* (non-Javadoc)
	 * @see javax.swing.tree.TreeNode#getAllowsChildren()
	 */
	public boolean getAllowsChildren() {
		return true;
	}

	/* (non-Javadoc)
	 * @see javax.swing.tree.TreeNode#isLeaf()
	 */
	public boolean isLeaf() {
	//	return getChildren().size() > 0;
		return getChildren().size() == 0;
	}

	/* (non-Javadoc)
	 * @see javax.swing.tree.TreeNode#children()
	 */
	public Enumeration children() {
		return (new Vector(getChildren())).elements();
	}

	public List getChildren() {
		if ((children == null) || isDirty()) {
			children = new ArrayList();

			try {
				StmtIterator iterator = model.listStatements();
				Statement statement;
				// RdfUtils_.show(model);
				while (iterator.hasNext()) {
					//	System.out.println("*");
					statement = (Statement)iterator.next();

					//    if(resource.equals(statement.getSubject())) {
					//System.out.println("\n+");
					//   System.out.println(
					//    	"ST = " + nodeMap.getTreeNode(statement));
					
					// @@ changed for Jena 2, was (RDFNode)statement
					children.add(nodeMap.getTreeNode(statement));
					//    }
				}
			} catch (Exception exception) {
				exception.printStackTrace();
			}
		}

		return children;
	}

	public String toString() {
		return resource.getLocalName();
	}

	public ImageIcon getIcon() {
		return TreeIcons.modelIcon;
	}

	/**
	 * Returns the resource.
	 * @return Resource
	 */
	public Resource getResource() {
		return resource;
	}

	/**
	 * Sets the resource.
	 * @param resource The resource to set
	 */
	public void setResource(Resource resource) {
		this.resource = resource;
	}

	/**
	 * Returns the nodeMap.
	 * @return NodeMap
	 */
	public RdfNodeMap getNodeMap() {
		return nodeMap;
	}

	/**
	 * Sets the nodeMap.
	 * @param nodeMap The nodeMap to set
	 */
	public void setNodeMap(RdfNodeMap nodeMap) {
		this.nodeMap = nodeMap;
	}

	public int getNodeType() {
		return RdfTreeNode.MODEL;
	}

	public void delete() {
	    /*
		String filename =
				RdfUtils
					.getFirstPropertyValue(resource, FILESYSTEM.local)
					.toString();
			Model childModel = RdfUtils.load(filename);
			
		getNodeMap().removeResource(resource);
			if (getParentStatement() != null) {
				getNodeMap().removeStatement(getParentStatement());
				removeStatement(getParentStatement());
			}
	*/
	}

	public void rename(String newName) {
	}
}
